﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using DataAccess;
using MySql.Data.MySqlClient;
using System.Data;
namespace BusinessFacade
{
   public class Busi_sys_user
    {
        public Busi_sys_user()
        {
        }
        public sys_user_Model Login(string UserName,string PassWord)
        {
            sys_user_Model model = new sys_user_Model();
            DataSet ds = new DataSet();
            string sql="select * from sys_user where LoginName='"+UserName+"'and LoginPassword='"+PassWord+"'";
            ds = DbHelperMySql.Query(sql);
            if(ds.Tables[0].Rows.Count>0)
            {
                if (ds.Tables[0].Rows[0]["UserID"] != null && ds.Tables[0].Rows[0]["UserID"].ToString() != "")
                {
                    model.UserID = int.Parse(ds.Tables[0].Rows[0]["UserID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["LoginName"] != null && ds.Tables[0].Rows[0]["LoginName"].ToString() != "")
                {
                    model.LoginName = ds.Tables[0].Rows[0]["LoginName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["LoginPassword"] != null && ds.Tables[0].Rows[0]["LoginPassword"].ToString() != "")
                {
                    model.LoginPassword = ds.Tables[0].Rows[0]["LoginPassword"].ToString();
                }
                if (ds.Tables[0].Rows[0]["AreaID"] != null && ds.Tables[0].Rows[0]["AreaID"].ToString() != "")
                {
                    model.AreaID = int.Parse(ds.Tables[0].Rows[0]["AreaID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["EmployeeID"] != null && ds.Tables[0].Rows[0]["EmployeeID"].ToString() != "")
                {
                    model.EmployeeID = int.Parse(ds.Tables[0].Rows[0]["EmployeeID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["LastLoginTime"] != null && ds.Tables[0].Rows[0]["LastLoginTime"].ToString() != "")
                {
                    model.LastLoginTime = DateTime.Parse(ds.Tables[0].Rows[0]["LastLoginTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CreateUserID"] != null && ds.Tables[0].Rows[0]["CreateUserID"].ToString() != "")
                {
                    model.CreateUserID = int.Parse(ds.Tables[0].Rows[0]["CreateUserID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CreateDate"] != null && ds.Tables[0].Rows[0]["CreateDate"].ToString() != "")
                {
                    model.CreateDate = DateTime.Parse(ds.Tables[0].Rows[0]["CreateDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ModifyUserID"] != null && ds.Tables[0].Rows[0]["ModifyUserID"].ToString() != "")
                {
                    model.ModifyUserID = int.Parse(ds.Tables[0].Rows[0]["ModifyUserID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ModifyDate"] != null && ds.Tables[0].Rows[0]["ModifyDate"].ToString() != "")
                {
                    model.ModifyDate = DateTime.Parse(ds.Tables[0].Rows[0]["ModifyDate"].ToString());
                }
                if(updateLoginTime(model.UserID, DateTime.Now)>0)
                {
                    return model;
                }
                
            }
            model = null;
            return model;
        }
        public int updateLoginTime(int UserID,DateTime LastLoginTime)
        {
            string sql = "update sys_user set LastLoginTime='"+LastLoginTime+"' where UserID="+UserID;
            int ReturnNum = DbHelperMySql.ExecuteSql(sql);
            return ReturnNum;
        }

        /// <summary>
        ///  增加一条数据
        /// </summary>
        public bool Add(sys_user_Model model)
        {
            int rowsAffected;
            MySqlParameter[] parameters = {
					new MySqlParameter("pUserID", MySqlDbType.Int32,11),
					new MySqlParameter("pLoginName", MySqlDbType.VarChar,200),
					new MySqlParameter("pLoginPassword", MySqlDbType.VarChar,50),
					new MySqlParameter("pAreaID", MySqlDbType.Int32,11),
					new MySqlParameter("pLastLoginTime", MySqlDbType.DateTime),
					new MySqlParameter("pCreateUserID", MySqlDbType.Int32,11),
					new MySqlParameter("pCreateDate", MySqlDbType.Datetime),
                                          };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.LoginName;
            parameters[2].Value = model.LoginPassword;
            parameters[3].Value = model.AreaID;
            parameters[4].Value = model.LastLoginTime;
            parameters[5].Value = model.CreateUserID;
            parameters[6].Value = model.CreateDate;

            DbHelperMySql.RunProcedure("sys_user_ADD", parameters);
            rowsAffected = int.Parse(parameters[0].Value.ToString());
            if(rowsAffected>0)
            {
                return true;
            }
            return false;
        }
        public void Update(sys_user_Model model)
        {
            MySqlParameter[] parameters = {
					new MySqlParameter("pUserID", MySqlDbType.Int32,11),
					new MySqlParameter("pLoginName", MySqlDbType.VarChar,200),
					new MySqlParameter("pLoginPassword", MySqlDbType.VarChar,50),
					new MySqlParameter("pAreaID", MySqlDbType.Int32,11),
					new MySqlParameter("pModifyUserID", MySqlDbType.Int32,11),
					new MySqlParameter("pModifyDate", MySqlDbType.Datetime),
                                          };
            parameters[0].Value = model.UserID;
            parameters[1].Value = model.LoginName;
            parameters[2].Value = model.LoginPassword;
            parameters[3].Value = model.AreaID;
            parameters[4].Value = model.ModifyUserID;
            parameters[5].Value = model.ModifyDate;

            DbHelperMySql.RunProcedure("sys_user_Update", parameters);
        }
       public DataTable GetList(string UserName,int AreaID)
       {
           DataSet ds = new DataSet();
             MySqlParameter[] parameters = {
                    new MySqlParameter("UserName", MySqlDbType.VarChar,50),
					new MySqlParameter("AreaID", MySqlDbType.Int32,5)};
             parameters[0].Value = UserName;
             parameters[1].Value = AreaID;
             ds = DbHelperMySql.RunProcedure("GetDropList", parameters);
             return ds.Tables[0];
       }
       public sys_user_Model GetListModel(int id)
       {
           sys_user_Model model = new sys_user_Model();
           string sql = "select * from sys_user where UserID="+id;
          //MySqlParameter [] parameters={
          //           new MySqlParameter("userID",MySqlDbType.Int32)
          //                             };
          //  parameters[0].Value=id;
          // DataSet ds = DbHelperMySql.RunProcedure("GetListModel",parameters);
           DataSet ds = DbHelperMySql.Query(sql);
          	if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["UserID"]!=null && ds.Tables[0].Rows[0]["UserID"].ToString()!="")
				{
					model.UserID=int.Parse(ds.Tables[0].Rows[0]["UserID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["LoginName"]!=null && ds.Tables[0].Rows[0]["LoginName"].ToString()!="")
				{
					model.LoginName=ds.Tables[0].Rows[0]["LoginName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["LoginPassword"]!=null && ds.Tables[0].Rows[0]["LoginPassword"].ToString()!="")
				{
					model.LoginPassword=ds.Tables[0].Rows[0]["LoginPassword"].ToString();
				}
				if(ds.Tables[0].Rows[0]["AreaID"]!=null && ds.Tables[0].Rows[0]["AreaID"].ToString()!="")
				{
					model.AreaID=int.Parse(ds.Tables[0].Rows[0]["AreaID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["EmployeeID"]!=null && ds.Tables[0].Rows[0]["EmployeeID"].ToString()!="")
				{
					model.EmployeeID=int.Parse(ds.Tables[0].Rows[0]["EmployeeID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["LastLoginTime"]!=null && ds.Tables[0].Rows[0]["LastLoginTime"].ToString()!="")
				{
					model.LastLoginTime=DateTime.Parse(ds.Tables[0].Rows[0]["LastLoginTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["CreateUserID"]!=null && ds.Tables[0].Rows[0]["CreateUserID"].ToString()!="")
				{
					model.CreateUserID=int.Parse(ds.Tables[0].Rows[0]["CreateUserID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["CreateDate"]!=null && ds.Tables[0].Rows[0]["CreateDate"].ToString()!="")
				{
					model.CreateDate=DateTime.Parse(ds.Tables[0].Rows[0]["CreateDate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["ModifyUserID"]!=null && ds.Tables[0].Rows[0]["ModifyUserID"].ToString()!="")
				{
					model.ModifyUserID=int.Parse(ds.Tables[0].Rows[0]["ModifyUserID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["ModifyDate"]!=null && ds.Tables[0].Rows[0]["ModifyDate"].ToString()!="")
				{
					model.ModifyDate=DateTime.Parse(ds.Tables[0].Rows[0]["ModifyDate"].ToString());
				}
                return model;
			}
           model=null;
           return model;
       }
       public static void Delete(int id)
       {
           MySqlParameter[] parameters = { 
                             new MySqlParameter("pUserID",MySqlDbType.Int32)
                                         };
           parameters[0].Value = id;
           DbHelperMySql.RunProcedure("sys_user_Delete", parameters);
       }
       }
    }
